<?php
include_once("functions/db_manipulate.php");

if (!init()) {
    redirect("login.php");
}

$sectionsResultSet = getSectionsByUserId();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Разделы форума</title>
    <link rel="stylesheet" href="css/navigation.css">
    <link rel="stylesheet" type="text/css" href="css/news.css">
    <link rel="stylesheet" type="text/css" href="css/square.css">
    <link rel="stylesheet" type="text/css" href="css/menu.css">
    <link rel="stylesheet" type="text/css" href="css/common.css">
    <link rel="stylesheet" type="text/css" href="css/sections.css">
    <script src="resources/jquery.js"></script>

    <meta charset="UTF-8">
</head>
<body class="body">
<?php
include_once("menu.php");
?>

<div id="square">
    <div class="sectiondiv">

        <?php

        $sectionHeader = "";

        while ($topic = mysql_fetch_assoc($sectionsResultSet)) {

            $lastCommentsResultSet = getLastComments($topic['id']);

            if ($sectionHeader != $topic['section']) {
                $sectionHeader = $topic['section'];
                ?>
                <div class="sectionheader">
                    <?= htmlspecialchars($sectionHeader) ?>
                </div>
            <?php

            }
            ?>
            <div class="sectiondiv">
                <div class="topicdiv">
                    <div class="topic" onclick="location.href='topic.php?tid=<?= $topic['id'] ?>&page=1'">
                        <div class="topicheader">
                            <?= htmlspecialchars($topic['header']) ?>
                        </div>
                        <div class="topicinfo">
                            <div class="topicinfodiv">
                                Автор: <?= $topic['author'] ?>
                            </div>
                            <div class="topicinfodiv">
                                Время: <?= $topic['time'] ?>
                            </div>
                        </div>

                        <div class="topicinfo">
                            <div class="topicinfodiv">
                                Дата: <?= $topic['date'] ?>
                            </div>
                            <div class="topicinfodiv">
                                Сообщений: <?php $msg = countTopicMessages($topic['id']);
                                echo $msg; ?>
                            </div>
                        </div>

                        <div class="topicinfo">
                            <div class="topicinfodiv">
                                Группа: <?= $topic['tgroup'] ?>
                            </div>
                            <div class="topicinfodiv">
                                Страницы:
                                <div id="pages">
                                    <?php
                                    $pages = pagesFromMessages($msg);
                                    $pagesStart = 0;
                                    $pagesEnd = 0;

                                    $page = $pages;

                                    if ($pages <= 5) {
                                        $pagesStart = 1;
                                        $pagesEnd = $pages;
                                    } else if ($pages >= 5 && $page + 2 <= $pages) {
                                        $pagesStart = $page - 2;
                                        $pagesEnd = $page + 2;
                                    } else if ($pages >= 5 && $page + 2 > $pages) {
                                        $pagesStart = $pages - 4;
                                        $pages;
                                    }

                                    if ($pages == 0) {
                                        ?>
                                        <div class="page"
                                             onclick="location.href='topic.php?tid=<?= $topic['id'] ?>&page=1'">
                                            0
                                        </div>
                                    <?php

                                    }

                                    if ($page > 1) {
                                        ?>
                                        <div class="page"
                                             onclick="location.href='news.php?tid=<?= $topic['id'] ?>&page=1'">
                                            <<
                                        </div>
                                    <?php
                                    }
                                    for ($i = $pagesStart; $i <= $pagesEnd; $i++) {
                                        ?>
                                        <div class="page"
                                             onclick="location.href='topic.php?tid=<?= $topic['id'] ?>&page=<?= $i ?>'">
                                            <?= $i ?>
                                        </div>
                                    <?php
                                    }
                                    if ($page != $pages) {
                                        ?>
                                        <div class="page"
                                             onclick="location.href='topic.php?tid=<?= $topic['id'] ?>&page=<?= $pages ?>'">
                                            >>
                                        </div>
                                    <?php
                                    }
                                    ?>
                                </div>
                            </div>
                        </div>
                    </div>


                    <?php
                    $lastComment = mysql_fetch_assoc($lastCommentsResultSet);
                    if ($lastComment) {

                        ?>
                        <div class="topiclast">
                            <?php
                            do {
                                ?>
                                <div class="lastcomment">
                                    <div class="lasttopiccommentinfo">
                                        <div class="lasttopiccommentinfodiv">
                                            Автор: <?= $lastComment['author'] ?>
                                        </div>
                                        <div class="lasttopiccommentinfodiv">
                                            Дата: <?= $lastComment['date'] ?>
                                        </div>
                                        <div class="lasttopiccommentinfodiv">
                                            Время: <?= $lastComment['time'] ?>
                                        </div>
                                    </div>
                                    <div class="commentpart">
                                        <?= htmlspecialchars($lastComment['content']) ?>
                                    </div>
                                </div>
                            <?php
                            } while ($lastComment = mysql_fetch_assoc($lastCommentsResultSet))
                            ?>
                        </div>
                    <?php

                    }
                    ?>


                </div>
            </div>
        <?php
        }
        ?>

        <form accept-charset="UTF-8" action="newtopic_handler.php" method="post"
              id="newtopicform">
            <span style="font-weight: bold">Создать новую тему:</span>
            <textarea name="header" id="textarea" rows="1" placeholder="News header.." required="true"></textarea>
            <textarea name="content" id="textarea" rows="3" placeholder="Type your legend here.."></textarea>

            <div style="width: 90%; margin-left: 20px;">
                Выберите раздел:
                <select name="section" id="select">
                    <?php
                    $sectionsResultSet = getSections();

                    while ($section = mysql_fetch_assoc($sectionsResultSet)) {
                        ?>
                        <option value="<?= $section['id'] ?>">
                            <?= htmlspecialchars($section['header']) ?>
                        </option>

                    <?php
                    }
                    ?>
                </select>
            </div>
            <br>

            <div style="display: inline-block; width: 100%; margin-left: 350px;">
                <div id="topicbutton" onclick="$('#newtopicform').submit()">
                    Создать
                </div>
            </div>
        </form>
    </div>
</div>

<?php
include_once("navigation.html");
?>
</body>
</html>
